/*
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You under the Apache License, Version 2.0
 *  (the "License"); you may not use this file except in compliance with
 *  the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package com.xmlutils.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**   
* @Title: MyUtils.java 
* @Package com.xmlutils.utils 
* @Description: 
* @author Tonsen
* @date 2021年5月17日 下午6:48:56 
* @version V1.0  
* @user Tonsen
* @update 
* 2021年5月17日 下午6:48:56 
*/
public class MyUtils {
	
	/**
	 * 在打印的每行信息前加入时间前缀
	 * @param message 待打印的信息（可多行）
	 */
	public static void printWithTimePrefix(String message) {
		System.out.println(getNowTimeMills() + "> " + message.replaceAll("\r\n", "\r\n" + getNowTimeMills() + "> "));
	}   
	
	
	/**
	 * 获取设备当前时间
	 * */
	public static String getNowTimeMills(){
		return new SimpleDateFormat("yyyy-MM-dd_HH:mm:ss.SSS").format(new Date());
	}
	
	/**
	 * 获取设备当前时间
	 * */
	public static String getNowTimeLine(){
		return new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss").format(new Date());
	}
	/**
	 * 获取设备当前时间
	 * */
	public static String getNowTimeLineMillis(){
		return new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss.SSS").format(new Date());
	}
	
	/**
	 * 获取设备当前时间
	 * */
	public static String getNowTime(){
		return new SimpleDateFormat("yyyy-MM-dd_HH:mm:ss").format(new Date());
	}
	
	/**
	 * 获取设备当前时间
	 * */
	public static String getNowTimeSpace() {
		return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
	}
	
	/**
	 * 获取设备当前时间
	 * */
	public static String getNowTimeUnderLine(){
		return new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss").format(new Date());
	}
	
	/**
	 * 把毫秒转成时间  XX天XX时XX分
	 * @param time 毫秒数
	 * @return 时分秒字符串
	 */
	public static String millisToTimeMinuteCn(long time) {  
		String timeStr = null;
		long dateL = 0;
		long hour = 0;  
		long minute = 0;  
		long second = 0;  
		if (time < 1000*60)  
			return "0天0时0分";  
		else {  
			second = time /1000;  
			minute = second / 60; 
			hour = minute /60;
			if (second < 60) {  
				timeStr = "0天0时0分";  
			}else if (minute < 60) {  
				timeStr = "0天0时" + unitFormat(minute) + "分";  
			} else if (hour < 24) {
				minute = minute - hour*60;  
				timeStr = "0天" + unitFormat(hour) + "时" + unitFormat(minute) + "分"; 
			} else {//数字>=3600 000的时候 (超过一天)
				dateL = hour / 24; //多少天
				hour = minute /60 - dateL*24; //多少小时 
				minute = minute - dateL*24*60 - hour*60;  
				timeStr = unitFormat(dateL) + "天" +  unitFormat(hour) + "时" + unitFormat(minute) + "分";  
			}  
		}  
		return timeStr;  
	}  
	/**
	 * 把毫秒转成时间  XX天XX时XX分XX秒xxx毫秒
	 * @param time 毫秒数
	 * @return 天 时 分 秒字符串
	 */
	public static String millisToTimeSecondCn(long time) {  
		String timeStr = null;
		long dateL = 0;
		long hour = 0;  
		long minute = 0;  
		long second = 0;  
		if (time < 1000)  
			return time + "毫秒";
		else {  
			second = time/1000;  
			minute = second / 60; 
			hour = minute /60;
			if (second < 60) {  
				timeStr = second + "秒";  
			}else if (minute < 60) { 
				second = second - minute * 60;
				timeStr = minute + "分" + unitFormat(second) + "秒";  
			} else if (hour < 24) {
				second = second - minute * 60;
				minute = minute - hour*60;  
				timeStr = hour + "时" + unitFormat(minute) + "分" + unitFormat(second) + "秒"; 
			} else {//数字>=3600 000的时候 (超过一天)
				dateL = hour / 24; //多少天
				hour = minute/60 - dateL*24; //多少小时 
				minute = minute - dateL*24*60 - hour*60; 
				second = second - dateL*24*60*60 - hour*60*60 - minute*60;
				timeStr = dateL + "天" +  unitFormat(hour) + "时" + unitFormat(minute) + "分" + unitFormat(second) + "秒";  
			} 
		}  
		return timeStr;  
	}  
	/**
	 * 把毫秒转成时间hh:mi:ss
	 * @param time 毫秒数
	 * @return 时分秒字符串
	 */
	public static String millisToTimeMinute(long time) {  
		String timeStr = null;  
		long hour = 0;  
		long minute = 0;  
		long second = 0;  
		if (time < 1000*60)  
			return "00:00";  
		else {  
			second = time /1000;  
			minute = second / 60;  
			if (second < 60) {  
				timeStr = "00:00";  
			}else if (minute < 60) {  
				timeStr = "00:" + unitFormat(minute);  
			}else{//数字>=3600 000的时候  
				hour = minute /60;  
				minute = minute % 60;  
				timeStr = unitFormat(hour) + ":" + unitFormat(minute);  
			}  
		}  
		return timeStr;  
	}  
	
	/**
	 * 把毫秒转成时间hh:mi:ss
	 * @param time 毫秒数
	 * @return 时分秒字符串
	 */
	public static String millisToTimeSecond(long time) {  
		String timeStr = null;  
		long hour = 0;  
		long minute = 0;  
		long second = 0;  
		if (time < 1000)  
			return "00:00:00";  
		else {  
			second = time /1000;  
			minute = second / 60;  
			if (second < 60) {  
				
				timeStr = "00:00:" + unitFormat(second);  
			}else if (minute < 60) {  
				second = second % 60;  
				timeStr = "00:" + unitFormat(minute) + ":" + unitFormat(second);  
			}else{//数字>=3600 000的时候  
				hour = minute /60;  
				minute = minute % 60;  
				second = second - hour * 3600 - minute * 60;  
				timeStr = unitFormat(hour) + ":" + unitFormat(minute) + ":" + unitFormat(second);  
			}  
		}  
		return timeStr;  
	}  
	
	/*把毫秒转成时间hh:mi:ss.xxx*/  
    public static String millisToTime(long time) {  
        String timeStr = null;  
        long hour = 0;  
        long minute = 0;  
        long second = 0;  
        long millisecond = 0;  
        if (time <= 0)  
            return "00:00:00.000";  
        else {  
            second = time /1000;  
            minute = second / 60;  
            millisecond = time % 1000;  
            if (second < 60) {  
                  
                timeStr = "00:00:" + unitFormat(second) + "." + unitFormat2(millisecond);  
            }else if (minute < 60) {  
                second = second % 60;  
                timeStr = "00:" + unitFormat(minute) + ":" + unitFormat(second) + "." + unitFormat2(millisecond);  
            }else{//数字>=3600 000的时候  
                hour = minute /60;  
                minute = minute % 60;  
                second = second - hour * 3600 - minute * 60;  
                timeStr = unitFormat(hour) + ":" + unitFormat(minute) + ":" + unitFormat(second) + "." + unitFormat2(millisecond);  
            }  
        }  
        return timeStr;  
    }  
  
    public static String unitFormat(long i) {//时分秒的格式转换  
        String retStr = null;  
        if (i >= 0 && i < 10)  
            retStr = "0" + Long.toString(i);  
        else  
            retStr = "" + i;  
        return retStr;  
    }  
  
    public static String unitFormat2(long i) {//毫秒的格式转换
        String retStr = null;  
        if (i >= 0 && i < 10)  
            retStr = "00" + Long.toString(i);  
        else if (i >=10 && i < 100) {  
             retStr = "0" + Long.toString(i);  
        }  
        else  
            retStr = "" + i;  
        return retStr;  
    }
	
	//日期转换为时间戳 (精确到毫秒), yyyy-MM-dd HH:mm:ss
	 public static long timeToStamp(String timers) {
		Date d = new Date();
		long timeStemp = 0;
		try {
			SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");
			d = sf.parse(timers);// 日期转换为时间戳
			timeStemp = d.getTime();
		} catch (ParseException e) {
		}
		return timeStemp;
	}
}
